package com.alan.demo;

import org.snmp4j.*;
import org.snmp4j.mp.SnmpConstants;
import org.snmp4j.smi.GenericAddress;
import org.snmp4j.smi.OID;
import org.snmp4j.smi.OctetString;
import org.snmp4j.smi.VariableBinding;
import org.snmp4j.transport.DefaultUdpTransportMapping;
import org.snmp4j.util.DefaultPDUFactory;
import org.snmp4j.util.TableEvent;
import org.snmp4j.util.TableUtils;

import java.io.IOException;
import java.util.List;

public class SnmpTest3 {

    public static void main(String[] args) {
        creatSnmp();
    }

    public static void creatSnmp() {

        TransportMapping transport = null;
        Snmp snmp = null;
        CommunityTarget target = null;
        try {
            transport = new DefaultUdpTransportMapping();
            snmp = new Snmp(transport);//创建snmp
            snmp.listen();//监听消息
            target = new CommunityTarget();
            target.setCommunity(new OctetString("public"));//社区名称
            target.setRetries(3);//连接次数
            target.setAddress(GenericAddress.parse("udp:192.168.4.139/161"));//监控的主机
            target.setTimeout(8000);//
            target.setVersion(SnmpConstants.version2c);
            String memory = collectMemory(snmp, target);
            System.out.println("内存使用率为：" + memory);
            String cpu = collectCPU(snmp, target);
            System.out.println("CPU利用率为：" + cpu);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (transport != null)
                    transport.close();
                if (snmp != null)
                    snmp.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    //获取内存相关信息
    public static String collectMemory(Snmp snmp, CommunityTarget target) {
        String memory = null;
        String[] oids = {"1.3.6.1.2.1.25.2.3.1.2", //type 存储单元类型
                "1.3.6.1.2.1.25.2.3.1.3", //descr
                "1.3.6.1.2.1.25.2.3.1.4", //unit 存储单元大小
                "1.3.6.1.2.1.25.2.3.1.5", //size 总存储单元数
                "1.3.6.1.2.1.25.2.3.1.6"}; //used 使用存储单元数;
        String PHYSICAL_MEMORY_OID = "1.3.6.1.2.1.25.2.1.2";//物理存储
        try {
            TableUtils tableUtils = new TableUtils(snmp, new DefaultPDUFactory(PDU.GETBULK));
            OID[] columns = new OID[oids.length];
            for (int i = 0; i < oids.length; i++)
                columns[i] = new OID(oids[i]);
            List<TableEvent> list = tableUtils.getTable(target, columns, null, null);
            for (TableEvent event : list) {
                VariableBinding[] values = event.getColumns();
                if (values == null) continue;
                int unit = Integer.parseInt(values[2].getVariable().toString());//unit 存储单元大小
                int totalSize = Integer.parseInt(values[3].getVariable().toString());//size 总存储单元数
                int usedSize = Integer.parseInt(values[4].getVariable().toString());//used 使用存储单元数
                String oid = values[0].getVariable().toString();
                if (PHYSICAL_MEMORY_OID.equals(oid)) {
                    memory = (long) usedSize * 100 / totalSize + "%";
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return memory;
    }

    //获取cpu使用率
    public static String collectCPU(Snmp snmp, CommunityTarget target) {
        String cpu = null;
        String[] oids = {"1.3.6.1.2.1.25.3.3.1.2"};
        try {
            TableUtils tableUtils = new TableUtils(snmp, new DefaultPDUFactory(PDU.GETBULK));
            OID[] columns = new OID[oids.length];
            for (int i = 0; i < oids.length; i++)
                columns[i] = new OID(oids[i]);
            List<TableEvent> list = tableUtils.getTable(target, columns, null, null);
            int percentage = 0;
            for (TableEvent event : list) {
                VariableBinding[] values = event.getColumns();
                if (values != null)
                    percentage += Integer.parseInt(values[0].getVariable().toString());
            }
            cpu = percentage / list.size() + "%";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return cpu;
    }


}
